package com.tairanchina.md.meeting.service.impl;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import java.util.List;
import com.tairanchina.md.meeting.dao.YearMeetingAwarderMapperExt;
import com.tairanchina.md.meeting.dao.YearMeetingAwardgoodsMapperExt;
import com.tairanchina.md.meeting.dto.AwarderDto;
import com.tairanchina.md.meeting.entity.*;
import com.tairanchina.md.meeting.service.YearMeetingAwarderService;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lxliuxuan on 2016/12/24.
 */
@Transactional
@Service
public class YearMeetingAwarderServiceImpl implements YearMeetingAwarderService {


	Logger logger = LoggerFactory.getLogger(YearMeetingAwarderServiceImpl.class);
	
	@Resource
	private YearMeetingAwarderMapperExt yearMeetingAwarderMapperExt;
	@Resource
	private YearMeetingAwardgoodsMapperExt awardgoodsMapperExt;
	@Override
	public int addAwarder(YearMeetingAwarder yearMeetingAwarder) {
		int result = 0;
		try {
			result = yearMeetingAwarderMapperExt.insertSelective(yearMeetingAwarder);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("记录用户中奖信息异常",e);
		}
		return result;
	}



	@Override
	public AwarderDto getAwarderList(Integer awardlevel) {
		AwarderDto awarderDto = new AwarderDto();



		YearMeetingAwarderExample example = new YearMeetingAwarderExample();
		YearMeetingAwarderExample.Criteria criteria = example.createCriteria();
		if(awardlevel != null){
			criteria.andAwardLevelEqualTo(awardlevel);
		}
		criteria.andIsdeletedEqualTo(0);
		example.setOrderByClause("award_level");
		List<YearMeetingAwarder> awarders = yearMeetingAwarderMapperExt.selectByExample(example);
		awarderDto.setAwarderNum(awarders.size());
		awarderDto.setList(awarders);
		return awarderDto;
	}

	@Override
	public int cancleAwarder(String phone) {
		YearMeetingAwarderExample example = new YearMeetingAwarderExample();
		YearMeetingAwarderExample.Criteria criteria = example.createCriteria();
		criteria.andUserPhoneEqualTo(phone);
		criteria.andIsdeletedEqualTo(0);
		List<YearMeetingAwarder> yearMeetingAwarders = yearMeetingAwarderMapperExt.selectByExample(example);

		if(yearMeetingAwarders != null && yearMeetingAwarders.size()>0){
			YearMeetingAwarder awarder = yearMeetingAwarders.get(0);
			awarder.setIsdeleted(1);
		/*	yearMeetingAwarderMapperExt.updateByExampleSelective(awarder,example);*/
			yearMeetingAwarderMapperExt.updateByPrimaryKeySelective(awarder);

			//同时将该中奖人所中奖品的数复原
			int awardLevel = awarder.getAwardLevel();

			YearMeetingAwardgoodsExample example1 = new YearMeetingAwardgoodsExample();
			example1.createCriteria().andAwardLevelEqualTo(awardLevel);
			List<YearMeetingAwardgoods> yearMeetingAwardgoodses = awardgoodsMapperExt.selectByExample(example1);

			if(yearMeetingAwardgoodses != null && yearMeetingAwardgoodses.size()>0){
				YearMeetingAwardgoods yearMeetingAwardgoods = yearMeetingAwardgoodses.get(0);
				yearMeetingAwardgoods.setAwardSurplusNum(yearMeetingAwardgoods.getAwardSurplusNum()+1);
				awardgoodsMapperExt.updateByPrimaryKeySelective(yearMeetingAwardgoods);

			}
			return awardLevel;

		}

		return 0;
	}
}
